import { useState, useEffect, useRef } from "react";
import { Modal, Alert, Tabs, Form, Select, DatePicker, Row, Col, Radio, Input, Space, Button, message } from "antd";
import { CommonModal } from "@/views/components/CommonModal";
import { reprintOutboundorder, getWaybillByCode } from "../service";
import { usePrintExpress } from "@/views/hooks/usePrintExpress";
import usePrint from "@/views/hooks/usePrint";

const PatchworkInvoice = props => {
	const { setData, handlePrint, onCancel } = props;

	const [form] = Form.useForm();
	const RefInput = useRef(null);
	const { printExpress } = usePrintExpress({
		callback: data => {
			console.log(data);
		}
	});
	const { printList, setPrintList, printOptions, getExpressRelativePrinter, setExpressRelativePrinter } = usePrint();

	function onOk() {
		form.validateFields().then(async values => {
			const API_DATA = form.getFieldsValue();
			const { success, data } = await getWaybillByCode({ code: API_DATA.uniqueCode });
			if (success) {
				const printConfig = getExpressRelativePrinter() || {};
				console.log(printConfig, data, 'co')
				if (!printConfig[data?.expressCompany]) {
					Modal.info({
						centered: true,
						title: "提示",
						content: `${data?.expressCompany}未设置打印机，请先设置打印机`
					});
				} else {
					reprintOutboundorder(API_DATA).then(res => {
						const { success, data } = res;
						if (success) {
							// 扫描唯一码之后，如果有面单直接打印，没有面单 提示：该订单未打印过，请直接扫码发货
							// 弹窗不关闭,清空数据
							//print string:'0'未打印 ‘1’已打印
							// if (data.print == '1') {
								// printExpress([data.outboundNo]);
								handlePrint([data.systemNo], printConfig[data?.expressCompany]);
							// } else {
								// Modal.info({ centered: true, content: "该订单未打印过，请直接扫码发货" });
								// message.warning("该订单未打印过，请直接扫码发货");
								// setData({});
							// }
						}
					});
				}
			}
		});
	}

	useEffect(() => {
		RefInput.current.focus();
	}, []);

	return (
		<CommonModal
			title={{ title: "补打面单" }}
			open={true}
			width="588px"
			onCancel={onCancel}
			footer={
				<Button type="primary" onClick={onOk}>
					补打面单
				</Button>
			}
		>
			<Alert
				message="注：扫描已验货采购标签补打快递面单，多件只需要扫描任意采购标签即可"
				showIcon
				type="info"
				style={{ marginBottom: "16px" }}
			/>
			<Form form={form}>
				<Form.Item name="uniqueCode" rules={[{ required: true, message: "请扫描商品唯一码" }]}>
					<Input ref={RefInput} placeholder="请扫描补打面单所包含的商品唯一码" style={{ height: 40 }} />
				</Form.Item>
			</Form>
		</CommonModal>
	);
};
export default PatchworkInvoice;
